package com.csx.framework.netty.sample.aio.client;

import com.csx.framework.netty.sample.aio.ChannelAdapter;
import com.csx.framework.netty.sample.aio.ChannelHandler;

import java.io.IOException;
import java.net.SocketAddress;
import java.nio.channels.AsynchronousSocketChannel;
import java.nio.charset.Charset;

/**
 * <p> what does this class do ? </p>
 * <p> How does it work ?</p>
 * <p> Why does it do this ?</p>
 *
 * @author cuisongxu
 * @date 2023/8/8 周二 17:14
 */
public class AioClientHandler extends ChannelAdapter {


    public AioClientHandler(AsynchronousSocketChannel channel, Charset charset) {
        super(channel, charset);
    }

    @Override
    public void channelActive(ChannelHandler ctx) {
        try {
            SocketAddress remoteAddress = ctx.channel().getRemoteAddress();
            System.out.println("客户端建立连接成功: remoteAddress: " + remoteAddress);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void channelInactive(ChannelHandler ctx) {

    }

    @Override
    public void channelRead(ChannelHandler ctx, Object msg) {

        ctx.writeAndFlush("客户端处理消息 " + msg + " success!");
    }
}
